//
//  XJHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 ZhangXJ. All rights reserved.
//

#define kColor (37 / 255.0)

#import "XJHomePageController.h"
#import "XJ_MenuViewController.h"
#import "XJNetWorkRequestManager.h"
#import "XJCollectionViewCell.h"
#import "XJCycleView.h"

@interface XJHomePageController () <MenuClickDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) XJCycleView *cycleView;
@end

@implementation XJHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:1];
    self.dataSource = [NSMutableArray new];
    
    //设置menu的内边距
    UITableView *menuTabelView = [XJ_MenuViewController getMenuViewController].leftTableView;
    menuTabelView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
    menuTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    menuTabelView.backgroundColor = [UIColor grayColor];

    
    [XJ_MenuViewController getMenuViewController].menuDelegate = self;

    //网络请求
    [XJNetWorkRequestManager RequestWithURL:[NSURL URLWithString:READLIST_URL] andMethod:Request_GET andParams:nil andResult:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSDictionary *dataDict = jsonDict[@"data"];
            
            for (NSDictionary *itemDic in dataDict[@"list"]) {
                XJHomeModel *model = [XJHomeModel new];
                [model setValuesForKeysWithDictionary:itemDic];
                [self.dataSource addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
    
    //创建FlowLayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height * 0.25);
    
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    
    //创建UICollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"XJCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
    // Do any additional setup after loading the view.
}


#pragma mark - UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //创建头部滚动视图
        self.cycleView = [[XJCycleView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) andInterVal:3];
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        self.cycleView.imgNameArray = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg"].mutableCopy;
        return header;
    }
    
    return nil;
}


//点击抽屉按钮
- (IBAction)didClickLeftBarButtonItem:(UIBarButtonItem *)sender {
    
    if ([XJ_MenuViewController getMenuViewController].isShowing) {
        [[XJ_MenuViewController getMenuViewController] hideLeftViewController];
    }else {
        [[XJ_MenuViewController getMenuViewController] showLeftViewController];
    }
    
}


#pragma mark - UICollectionViewDataSource方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
